<template>
  <view>
    <!--父组件中在子组件身上绑定了自定义事件，想要触发这个在父组件身上的方法，需要用$emit方法在子组件中触发 -->
    <my-search @onclick="gotoSearch"></my-search>
   <!--  <my-search :bgcolor="'#000000'" :raduis="1"></my-search> -->
    <view class="scroll-box">
      <!-- 左侧的滑动区域 -->
      <scroll-view class="left-view" scroll-y="true" :style="{height: wh+'px'}">
        <view :class="['left-view-item', index1===active?'active':'']" v-for="(item1,index1) in cateList" :key="index1"
          @click="activeChange(index1)">{{item1.cat_name}}</view>
      </scroll-view>
      <!-- 右侧的滑动区域 -->
      <scroll-view :scroll-top="scrollTop" class="right-view" scroll-y="true" :style="{height: wh+'px'}">
        <view class="right-view-title" v-for="(item2,index2) in cateList2" :key="index2">
          /{{item2.cat_name}}/
          <view class="right-view-list">
            <view class="right-view-item" v-for="(item3,index3) in item2.children" :key="index3" @click="goGoodsList(item3)">
              <image :src="item3.cat_icon"></image>
              <text>{{item3.cat_name}}</text>
            </view>
          </view>
        </view>


      </scroll-view>

    </view>

  </view>
</template>

<script>
  export default {
    data() {
      return {
        wh: 0,
        active: 0,
        scrollTop:0,
        cateList: [],
        cateList2: [],
      }
    },
    onLoad() {
      // 获取窗口可用高度
      const sysinfo = uni.getSystemInfoSync()
      console.log(sysinfo)
      this.wh = sysinfo.windowHeight-50
      // 获取分类列表
      this.getCateList()

    },
    methods: {
      // 获取一级分类列表数据
      async getCateList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/categories')
        if (res.meta.status !== 200) return uni.$showMsg()

        this.cateList = res.message

        this.cateList2 = this.cateList[0].children

      },
      // 获取二级分类列表数据

      getCateList2(i) {
        this.cateList2 = this.cateList[i].children
      },
      // 一级分类点击事件
      activeChange(i) {
        this.active = i
        this.getCateList2(i)
        this.scrollTop=this.scrollTop===0?1:0
      },
      
      goGoodsList(item3){
        uni.navigateTo({
          url:`../../subpkg/goods_list/goods_list?cid=${item3.cat_id}`
        })
      },
       // 跳转到分包中的搜索页面
         gotoSearch() {
           uni.navigateTo({
             url: '/subpkg/search/search'
           })
         }

    }


  }
</script>

<style lang="scss">
  .scroll-box {
    display: flex;

    .left-view {
      width: 120px;

      .left-view-item {
        line-height: 60px;
        background-color: #f7f7f7;
        text-align: center;
        font-size: 12px;

        // 激活项的样式使用多类选择器
        &.active {
          background-color: #ffffff;
          position: relative;

          &::before {
            content: ' ';
            display: block;
            width: 3px;
            height: 30px;
            background-color: #c00000;
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);

          }

        }
      }
    }

    .right-view {
      background-color: #ffffff;

      .right-view-title {
        font-size: 12px;
        font-weight: bold;
        text-align: center;
        padding: 15px 0;
      }

      .right-view-list {
        display: flex;
        flex-wrap: wrap;
        margin: 10px 0;

        .right-view-item {
          width: 33.33%;
          display: flex;
          flex-direction: column;
          align-items: center;


          image {
            width: 60px;
            height: 60px;
          }

          text {
            margin-top: 5px;
            font-size: 12px;
          }
        }
      }

    }
  }
</style>
